package com.lbh;

import com.alibaba.fastjson.JSON;

import java.io.*;

/***
 * project:JRuntime
 * package:com.lbh
 *
 * @author user:lenovo QQ:1601078366
 * @date 2021/10/22 0:59
 * @version V1.0
 */
public class Utility {

    public static boolean storeJSON(Object o, String res){
        File f = new File(Utility.class.getResource("/").getFile() + res);
        PrintWriter os;
        if (!f.exists()){
            try {
                if (!f.createNewFile()) {
                    return false;
                }
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        try {
            os = new PrintWriter(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        }
        os.print(JSON.toJSONString(o, true));
        os.close();
        return true;
    }

    public static <T> T loadJSON(String res, Class<T> clazz){
        InputStream is = Utility.class.getResourceAsStream(res);
        if(is == null){
            return null;
        }
        T ret;
        try {
            ret = JSON.parseObject(is, clazz);
        } catch (IOException | ClassCastException e) {
            e.printStackTrace();
            return null;
        }
        return ret;
    }
}
